﻿namespace KinonekoSoftware.Studio.Models.Modules
{
    public sealed class TextElementPresenter : ElementPresenter<Element_Text>
    {
        protected override void OnInitialize(Element_Text _)
        {
            RaiseUpdated(nameof(Value));
            RaiseUpdated(nameof(Metadata));
            RaiseUpdated(nameof(Fallback));
            RaiseUpdated(nameof(Suffix));
        }
        
        /// <summary>
        /// 获得当前内容块的标题。
        /// </summary>
        /// <returns>返回当前内容块的标题。</returns>
        public override string GetTitle() => string.IsNullOrEmpty(Title) ? CodariaSR.Module_Block_Text : Title;
        public override bool IsMultipleVariants() => false;
        public override IEnumerable<Variant> GetVariants(string documentID) => null;
        public override Variant GetVariant(string documentID)
        {
            return new StringVariant
            {
                Id       = documentID,
                Metadata = Metadata,
                Value    = Value,
                Type     = VariantType.String,
            };
        }

        public override void DataSync(Variant variant)
        {
            if (variant is StringVariant sv)
            {
                Value = sv.Value;
            }
        }

        public string Value
        {
            get => AssociatedObject?.Value;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Value = value;
                RaiseUpdated();
                RaisePropertyUpdated(Metadata);
            }
        }

        public string Fallback
        {
            get => AssociatedObject?.Fallback;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Fallback = value;
                RaiseUpdated();
            }
        }

        public string Metadata
        {
            get => AssociatedObject?.Metadata;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }


                RaiseMetadataChanged(AssociatedObject.Metadata, value);
                AssociatedObject.Metadata = value;
                RaiseUpdated();
            }
        }



        public string Suffix
        {
            get => AssociatedObject?.Suffix;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Suffix = value;
                RaiseUpdated();
            }
        }
    }
}